package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealCheckgroupDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.SetmealCheckgroup;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealDao setmealDao;
    @Resource
    private SetmealCheckgroupDao setmealCheckgroupDao;

    @Transactional
    @Override
    public void save(Setmeal setmeal, List<Integer> checkgroupIds) {
        setmealDao.save(setmeal);

        List<SetmealCheckgroup> list = new ArrayList<>();
        checkgroupIds.forEach(checkgroupId -> {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            list.add(setmealCheckgroup);
        });
        setmealCheckgroupDao.save(list);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data：{}", queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 分页查询数据库page = {Page@7298}  size = 10
        Page<Setmeal> page = setmealDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.selectbyId(id);
    }

    @Transactional
    @Override
    public void edit(Setmeal setmeal, List<Integer> checkGroupIds) {
        setmealDao.update(setmeal);

        if (CollectionUtils.isEmpty(checkGroupIds)) {
            setmealCheckgroupDao.deleteBySetmealId(setmeal.getId());
        } else {
            List<SetmealCheckgroup> list = new ArrayList<>();
            checkGroupIds.forEach(checkGroupId -> {
                SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
                setmealCheckgroup.setSetmealId(setmeal.getId());
                setmealCheckgroup.setCheckgroupId(checkGroupId);
                list.add(setmealCheckgroup);
            });
            setmealCheckgroupDao.deleteBySetmealId(setmeal.getId());
            setmealCheckgroupDao.save(list);
        }
    }

    @Transactional
    @Override
    public void deleteSetmealById(Integer id) {
            setmealDao.deleteById(id);

            List<SetmealCheckgroup> list = setmealCheckgroupDao.selectBySetmealId(id);
            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            setmealCheckgroupDao.deleteSetmealById(id);
    }

    @Override
    public List<Setmeal> getSetmealAll() {
        List<Setmeal> setmealList = setmealDao.getSetmealAll();
        return setmealList;
    }
}
